package org.deri.socializer.com.xmpp;

import java.util.HashMap;

import org.deri.socializer.activities.chat.Participant;
import org.deri.socializer.activities.chat.ParticipantGroupChat;
import org.deri.socializer.util.XMPPUtilities;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.muc.MultiUserChat;

public class XmppGroupChat extends XmppChat {

	private XmppClient xmppClient;
	private MultiUserChat chat;

	private String roomName;


	
	public String getRoomName() {
		return roomName;
	}


//	public XmppGroupChat(XmppClient xmppClient) {
//		this.xmppClient = xmppClient;
//		this.participants = new HashMap<String, Participant>();
//		this.roomName = "---dummy---";
//		this.conversationHistory = new XmppConversationHistory(this);
//	}
	
	public XmppGroupChat(XmppClient xmppClient, String room) {
		this.xmppClient = xmppClient;
		this.participants = new HashMap<String, Participant>();
		this.roomName = room;
		this.jid = room + "@" + this.xmppClient.getXMPPUser().getResource() +  "." + this.xmppClient.getXMPPUser().getHost();
		this.conversationHistory = new XmppConversationHistory(this);
	}
	
	public void invite(String user, String reason) {
		if (this.chat == null)
			return;
		this.chat.invite(user, reason + this.chat.getRoom());
	}

	public boolean join() {
		if (this.chat != null)
			leave();
		this.roomName = XMPPUtilities.getRoomName(this.jid);
		this.participants.clear();
		this.chat = new MultiUserChat(this.xmppClient.getXMPPConnection(), this.jid);

		try {
			chat.join(this.xmppClient.getXMPPUser().getUser());
			try {
				Form answer = new Form(Form.TYPE_SUBMIT);
				chat.sendConfigurationForm(answer);
			} catch (Exception exc) {
				// DO NOTHING: occurs if room already exists (configuration/answer form only allowed for creator of room)
			}
			return true;
		} catch (XMPPException e) {
			System.err.println("Error: unable to join group chat " + this.jid);
			this.roomName = "";
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean leave() {
		if (this.chat != null) { 
			this.chat.leave();
		}
		return true;
	}
	
	public boolean isJoined() {
		if (this.chat == null)
			return false;
		return this.chat.isJoined();
	}
	
	public void sendMessage(String subjects, String text) throws XMPPException {
		if ((this.chat == null) || (!this.chat.isJoined()))
			return;
		this.chat.sendMessage(text);
		this.conversationHistory.addMessage(new Message(text));
	}
	

	public void handleMessage(Message message) {
		this.conversationHistory.addMessage(message);
		//this.xmppClient.handleWebGroupChatMessage(message);
	}
	
	public void handlePresence(Presence presence) {
		String userJID = presence.getFrom().toString();
		if (presence.getType().toString().equalsIgnoreCase("available"))
			synchronized (participants) {	
				this.participants.put(userJID, new ParticipantGroupChat(userJID, presence.getStatus()));
			}
		else 
			synchronized (participants) {
				this.participants.remove(userJID);
			}
	}
	

	public HashMap<String, Participant> getParticipants() {
		return this.participants;
	}
	
	public String getCurrentRoom() {
		return this.roomName;
	}
	

}
